﻿using System;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class ToggleCtrl : MonoBehaviour
{
    public float RefreshTime;           // 按钮刷新时间
    private float lastTime;           // 按钮操作时间
    private int childCount;           // 上次按钮数量

    /// <summary>
    /// 当前高亮显示的索引
    /// </summary>
    public int CurHighIdx { get; private set; }
    /// <summary>
    /// 标签改变事件
    /// </summary>
    public Action<int> TabChanged;
    /// <summary>
    /// 当前按钮列表
    /// </summary>
    List<ToggleBtn> tabBtnList = new List<ToggleBtn>();

    /// <summary>
    /// 初始化需要高亮的按钮
    /// </summary>
    /// <param name="idx"></param>
    void InitHightTab(int idx)
    {
        childCount = tabBtnList.Count;
        for (int i = 0; i < childCount; i++)
        {
            tabBtnList[i].idx = i;
            tabBtnList[i].SetTabEnable(idx == i);
        }

        if (idx >= 0 && idx < tabBtnList.Count)
        {
            CurHighIdx = idx;
            TabChanged?.Invoke(idx);
        }
    }

    /// <summary>
    /// 切换按钮
    /// </summary>
    /// <param name="idx"></param>
    void DoChangeTab(int idx)
    {
        if (CurHighIdx >= 0 && CurHighIdx < tabBtnList.Count)
        {
            tabBtnList[CurHighIdx].SetTabEnable(false);
        }

        if (idx >= 0 && idx < tabBtnList.Count)
        {
            tabBtnList[idx].SetTabEnable(true);
            CurHighIdx = idx; TabChanged?.Invoke(idx);
        }
    }

    /// <summary>
    /// 切换标签
    /// </summary>
    /// <param name="idx"></param>
    public void OnChangeTab(int idx)
    {
        GetComponentsInChildren(true, tabBtnList);
        if (tabBtnList.Count != childCount)
        {
            InitHightTab(idx);
        }
        else { DoChangeTab(idx); }
    }

    /// <summary>
    /// 按钮点击的处理
    /// </summary>
    /// <param name="idx"></param>
    public void OnTabClicked(int idx)
    {
        if (Time.time - lastTime >= RefreshTime)
        {
            lastTime = Time.time;
            if (CurHighIdx != idx)
            {
                DoChangeTab(idx);
            }
        }
    }

    /// <summary>
    /// 是否开启按钮点击
    /// </summary>
    /// <param name="enabled"></param>
    public void SetTabState(bool enabled)
    {
        for (int i = 0; i < tabBtnList.Count; i++)
        {
            tabBtnList[i].SetTabState(enabled);
        }
    }

    /// <summary>
    /// 设置按钮显示
    /// </summary>
    /// <param name="idx"></param>
    public void SetTabShow(int idx, bool active)
    {
        if (idx >= 0 && idx < tabBtnList.Count)
        {
            tabBtnList[idx].SetTabActive(active);
        }
    }

    /// <summary>
    /// 设置按钮文本
    /// </summary>
    /// <param name="idx"></param>
    /// <param name="name"></param>
    public void SetTabName(int idx, string name)
    {
        if (idx >= 0 && idx < tabBtnList.Count)
        {
            tabBtnList[idx].SetTabName(name);
        }
    }

    /// <summary>
    /// 设置按钮红点显隐
    /// </summary>
    /// <param name="idx"></param>
    /// <param name="active"></param>
    public void SetTabRedPoint(int idx, bool active)
    {
        if (idx >= 0 && idx < tabBtnList.Count)
        {
            tabBtnList[idx].SetTabRedPoint(active);
        }
    }
}